<template>
  <view class="notice-page">


    <!-- 列表 -->
    <scroll-view
      scroll-y
      class="list-wrapper"
      refresher-enabled
      :refresher-triggered="triggered"
      @refresherrefresh="onRefresh"
      @scrolltolower="loadMore"
    >
      <!-- 空占位 -->
      <u-empty v-if="!loading && notices.length === 0" text="暂无通知" />

      <!-- 卡片 -->
      <view
        v-for="(item, idx) in notices"
        :key="idx"
        :class="['item', { unread: item.unread }]"
        @tap="goDetail(item)"
      >
        <view class="item-title">{{ item.title }}</view>
		
		<view class="item-content-wrapper">
		  <mp-html
		    class="item-content"
		    :content="item.content"
		  />
		</view>
        <view class="item-time">{{ item.createTime }}</view>
      </view>

      <!-- 底部提示 -->
      <view v-if="loading" class="load-tip">正在加载...</view>
      <view v-if="noMore" class="load-tip">没有更多了~</view>
    </scroll-view>

    <!-- 底部 tabbar -->
    <u-tabbar
      :value="currentTab"
      :placeholder="false"
      :safeAreaInsetBottom="false"
    >
      <u-tabbar-item
        v-for="(tab, index) in tabBarList"
        :key="index"
        :text="tab.text"
        :name="tab.text"
        @click="switchTab(tab)"
      >
        <image
          slot="active-icon"
          class="bar-img"
          :src="tab.selectedIconPath"
        />
        <image
          slot="inactive-icon"
          class="bar-img"
          :src="tab.iconPath"
        />
      </u-tabbar-item>
    </u-tabbar>
  </view>
</template>

<script>
export default {
  data() {
    return {
      page: 1,
      pageSize: 10,
      loading: false,
      noMore: false,
      triggered: false,
      notices: [],
      currentTab: '通知',
      tabBarList: this.$store.state.dynamicTabbar
    };
  },
  onShow() {
    this.fetch(); // 首次加载
		  console.log(this.tabBarList)
  },
  methods: {
    /* 获取数据 */
    async fetch(reset = false) {
      if (reset) {
        this.page = 1;
        this.noMore = false;
      }

      if (this.loading || this.noMore) return;
      this.loading = true;

      try {
       const res = await uni.$http.get('/sys/notification/page', {
       			pageNo: this.page,
       			pageSize: this.pageSize
       });
	
        if (reset) this.notices = res.data.data.data.rows;
        else this.notices.push(...res.data.data.data.rows);
		

        if (this.notices.length >= res.data.data.data.total) this.noMore = true;
        else this.page++;
      } catch (e) {
		  console.log(e);
        uni.showToast({ title: '加载失败', icon: 'none' });
      } finally {
        this.loading = false;
        this.triggered = false;
      }
    },

    /* 下拉刷新 */
    onRefresh() {
      this.triggered = true;
      this.fetch(true);
    },

    /* 触底加载 */
    loadMore() {
      this.fetch();
    },

    /* 跳转详情 */
    goDetail(item) {
		uni.navigateTo({
		  url: '/pages/noticeDetail/noticeDetail?item=' +
		       encodeURIComponent(JSON.stringify(item))
		});
	
    },

    /* 切换 tab */
    switchTab(tab) {
		console.log(tab)
      uni.switchTab({ url: tab.pagePath });
    }
  }
};
</script>

<style lang="scss" scoped>
$primary: #3da9f5;

.notice-page {
  height: 100vh;
  display: flex;
  flex-direction: column;
  background: #f5f7fa;
}

.list-wrapper {
  flex: 1;
  padding: 15px;

  .item {
    background: #fff;
    border-radius: 12rpx;
    padding: 30rpx;
    margin-bottom: 20rpx;
    box-shadow: 0 4rpx 12rpx rgba($primary, 0.08);

    &.unread {
      border-left: 6rpx solid $primary;
    }

    .item-title {
      font-size: 32rpx;
      font-weight: 600;
      color: #333;
    }

    .item-content {
      margin-top: 10rpx;
      font-size: 28rpx;
      color: #666;
      line-height: 1.5;
    }

    .item-time {
      margin-top: 20rpx;
      font-size: 24rpx;
      color: #999;
      text-align: right;
    }
  }

  .load-tip {
    text-align: center;
    font-size: 26rpx;
    color: #aaa;
    padding: 20rpx 0;
  }
}
.item-content-wrapper {
  /* 限制一行，宽度随父级 */
  width: 100%;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  font-size: 28rpx;
  color: #666;
  line-height: 1.5;
}
.bar-img {
  width: 54rpx;
  height: 54rpx;
}
</style>